package soundwave.com.demo.list;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.input.InputManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import soundwave.com.demo.R;
import soundwave.com.demo.activity.Login;
import soundwave.com.demo.adpater.ViewpagerList_item_adapter;
import soundwave.com.demo.entry.LoginSingle;
import soundwave.com.demo.test.ViewpagerList_item_test;

/**
 * Created by xu on 2017/4/16.
 */

public class ViewpagerList_item extends Activity {
    private TextView back;
    private TextView title;
    private EditText search;
    private ListView item_list;
    private ArrayList<ViewpagerList_item_test> list;
    private Context context = this;
    private PopupWindow popupWindow;
    private LoginSingle loginSingle;
    private View viewlayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpagerlist_item_list);
        initView();
        onclik();
        loginSingle = LoginSingle.getInstance();
        ViewpagerList_item_test viewpagerList_item_test = getIntent().getParcelableExtra("viewpagerList_item_test");
        title.setText(viewpagerList_item_test.getTitle());

        list = new ArrayList<>();
        ViewpagerList_item_test viewpagerList_item_test1 = new ViewpagerList_item_test();
        viewpagerList_item_test1.setConntent("龙华街道");
        list.add(viewpagerList_item_test1);
        ViewpagerList_item_test viewpagerList_item_test2 = new ViewpagerList_item_test();
        viewpagerList_item_test2.setConntent("白石龙街道");
        list.add(viewpagerList_item_test2);

        ViewpagerList_item_adapter adapter = new ViewpagerList_item_adapter(this, R.layout.viewpagerlist_item_conntent, list);
        item_list.setAdapter(adapter);
        adapter.notifyDataSetChanged();


    }

    private void initView() {
        back = (TextView) findViewById(R.id.back);
        title = (TextView) findViewById(R.id.title);
        search = (EditText) findViewById(R.id.search);
        item_list = (ListView) findViewById(R.id.item_list);
        viewlayout = (View) findViewById(R.id.viewlayout);

    }

    private void onclik() {
        item_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                showPopupwindows();

            }
        });
        item_list.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                InputMethodManager imm= (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

                return imm.hideSoftInputFromWindow(item_list.getWindowToken(),0);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void showPopupwindows() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view1 = inflater.inflate(R.layout.popupwindow_viewpager_content, null);
        popupWindow = new PopupWindow(view1, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setContentView(view1);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        viewlayout.setVisibility(View.VISIBLE);
        TextView title = (TextView) view1.findViewById(R.id.title);
        Button tellphone = (Button) view1.findViewById(R.id.tellphone);
        Button sendmessage = (Button) view1.findViewById(R.id.sendmessage);
        TextView exit = (TextView) view1.findViewById(R.id.exit);

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                viewlayout.setVisibility(View.GONE);
            }
        });
        tellphone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loginSingle.isLogin()) {
                    Toast.makeText(context, "拨打电话", Toast.LENGTH_SHORT).show();
                    popupWindow.dismiss();
                } else {
                    startActivity(new Intent(context, Login.class));
                }
            }
        });
        sendmessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loginSingle.isLogin()) {
                    Toast.makeText(context, "发送短信", Toast.LENGTH_SHORT).show();
                    popupWindow.dismiss();
                } else {
                    startActivity(new Intent(context, Login.class));
                }
            }
        });
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        View view2 = LayoutInflater.from(context).inflate(R.layout.viewpagerlist_item_list, null);
        popupWindow.showAtLocation(view2, Gravity.BOTTOM, 0, 0);

    }


}
